package com.leo;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @ClassName WritableImageDemo
 * @Description 截图功能
 * @Author wangss
 * @date 2020.06.02 21:47
 * @Version 1.0
 */
public class WritableImage3Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        Image image2 = new Image("images/19.jpeg");

        WritableImage writableImage = new WritableImage(image2.getPixelReader(), 1200, 1200);

        ImageView imageView = new ImageView(writableImage);

        // 这是截图
        WritableImage snapshot = imageView.snapshot(null, null);

        an.getChildren().add(imageView);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageViewDemo");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.show();

        // 将处理以后的照片写到磁盘上，这里注意：这里处理以后的图片是 32位的，jpg是24位的，所以要用png保存
        BufferedImage bufferedImage = SwingFXUtils.fromFXImage(snapshot, null);
        ImageIO.write(bufferedImage, "png", new File("D:/log/newImage1.png"));
    }
}
